let moneyList=[
  {value:30,isSelect:false},
  { value: 50, isSelect: false },
  { value: 100, isSelect: false },
  { value: 200, isSelect: false },
  { value: 500, isSelect: false },
  { value: 1000, isSelect: false },
];
let ajax = getApp().globalData.ajax;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance:0,
    moneyList: moneyList,
    money:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      balance:options.balance
    })
  },
  selectMoney(e){
      console.log(e);
      let index = e.currentTarget.dataset.index;
      let moneyList = this.data.moneyList.slice();
      for (let i = 0; i < moneyList.length;i++){
        if (i===index){
          moneyList[i].isSelect = true;
          this.setData({
            money:moneyList[i].value,
          })
        }else{
          moneyList[i].isSelect = false;
        }
      }
      this.setData({
        moneyList,
      })
  },
  otherMoney(e){
    let money = Number(e.detail.value);
    if(!isNaN(money)){
      this.setData({ money})
    }else{
      wx.showToast({
        icon:'none',
        title:'请输入正确的金额'
      })
    }
  },
  payment(){
    let json={
      token: wx.getStorageSync('Cookie'),
      price: this.data.money,
      order_type:'ChargeOrder',
    }
    ajax('/pay/add', json).then(res=>{
      let payInfo = res.data;
      let json={
        token: wx.getStorageSync('Cookie'),
        pay_type: 'WEI_XIN',
        order_type: 'ChargeOrder',
        order_no: res.data.order_no,
      }
      ajax('/pay/info', json).then(res=>{
        let data=res.data.msg;
        console.log(data);
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: 'MD5',
          paySign: data.paySign,
          success(){
            wx.showToast({
              title: '充值成功',
              image: '',
              duration: 0,
              mask: true,
              icon:'success',
            })
          },
          fail() {
            wx.showToast({
              title: '充值失败',
              image: '',
              duration: 0,
              mask: true,
              icon: 'success',
            })
          }
      })
      
      })

    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})